// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Eggman Harpoon Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value1 : object.originPos.x
private alias object.value2 : object.originPos.y

// States
private alias 0 : OOZEGGMANHARPOON_SETUP
private alias 1 : OOZEGGMANHARPOON_MOVERIGHT
private alias 2 : OOZEGGMANHARPOON_MOVELEFT


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case OOZEGGMANHARPOON_SETUP
		if object.propertyValue == 0
			object.originPos.x = object.xpos
			object.originPos.x += 0x800000
			object.originPos.y = object.ypos
			object.state = OOZEGGMANHARPOON_MOVELEFT
		else
			object.originPos.x = object.xpos
			object.originPos.x -= 0x800000
			object.originPos.y = object.ypos
			object.state = OOZEGGMANHARPOON_MOVERIGHT
		end if
		temp1 = object.angle
		break

	case OOZEGGMANHARPOON_MOVERIGHT
		object.angle += 2
		object.angle &= 0x1FF
		if object.angle == 0
			object.type = TypeName[Blank Object]
		end if
		temp1 = object.angle
		temp1 += 12
		break

	case OOZEGGMANHARPOON_MOVELEFT
		object.angle -= 2
		object.angle &= 0x1FF
		if object.angle == 0
			object.type = TypeName[Blank Object]
		end if
		temp1 = object.angle
		temp1 -= 12
		break
		
	end switch

	Sin(object.xpos, object.angle)
	object.xpos *= 0x3400
	object.xpos += object.originPos.x

	Cos(object.ypos, object.angle)
	object.ypos *= 0x3400
	object.ypos += object.originPos.y

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			CallFunction(Player_Hit)
		end if
	next

	Sin(object.xpos, temp1)
	object.xpos *= 0x3400
	object.xpos += object.originPos.x

	Cos(object.ypos, temp1)
	object.ypos *= 0x3400
	object.ypos += object.originPos.y

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			CallFunction(Player_Hit)
		end if
	next
end event


event ObjectDraw
	temp1 = object.angle
	if object.state == OOZEGGMANHARPOON_MOVELEFT
		temp1 += 108
		temp1 &= 0x1FF
		temp0 = 0
		while temp0 < 9
			Sin(object.xpos, temp1)
			object.xpos *= 0x3400
			object.xpos += object.originPos.x

			Cos(object.ypos, temp1)
			object.ypos *= 0x3400
			object.ypos += object.originPos.y

			DrawSprite(0)
			
			temp0++
			temp1 -= 12
			temp1 &= 0x1FF
		loop
		object.direction = FLIP_X
		object.rotation = 0x180
		object.rotation -= object.angle
		object.rotation &= 0x1FF
	else
		temp1 -= 108
		temp1 &= 0x1FF
		temp0 = 0
		while temp0 < 9
			Sin(object.xpos, temp1)
			object.xpos *= 0x3400
			object.xpos += object.originPos.x
			
			Cos(object.ypos, temp1)
			object.ypos *= 0x3400
			object.ypos += object.originPos.y

			DrawSprite(0)

			temp0++
			temp1 += 12
			temp1 &= 0x1FF
		loop
		object.direction = FLIP_NONE
		object.rotation = 0x080
		object.rotation -= object.angle
		object.rotation &= 0x1FF
	end if

	DrawSpriteFX(1, FX_ROTATE, object.xpos, object.ypos)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("OOZ/Objects.gif")
		SpriteFrame(-8, -8, 16, 16, 379, 196)
		SpriteFrame(-8, -34, 16, 31, 379, 213)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-8, -8, 16, 16, 355, 214)
		SpriteFrame(-8, -34, 16, 31, 360, 50)
	end if
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("OOZ/Objects.gif")
		SpriteFrame(-8, -34, 16, 31, 379, 213)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-8, -34, 16, 31, 360, 50)
	end if
	
	// Although used by the object, it's not to be set from the editor (or placed into a scene at all)
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
